﻿using AutoMapper;
using Common;
using CommonModel;
using CommonModel.Config;
using CommonModel.Constant;
using Domain.Database;
using Domain.DbContext;
using IBll;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace Bll
{
    /// <summary>
    /// 配置服务
    /// </summary>
    public class ConfigBll : IConfigBll
    {
        public ModelContainer CurrentDbContext { get; set; }
        public ICacheBll CacheBll { get; set; }

        /// <summary>
        /// 修改配置服务
        /// </summary>
        /// <param name="configRequest"></param>
        /// <returns></returns>
        public bool UpdateConfig(ConfigRequest configRequest)
        {
            bool result = false;

            Config config = Mapper.Instance.Map<Config>(configRequest);
            string operatorUsername = GetOperatorUsername();
            if (CurrentDbContext.Configs.Count(a => a.Key == configRequest.Key) > 0)
            {
                config.ModifyUser = operatorUsername;
                config.ModifyTime = DateTime.Now;
                CurrentDbContext.Entry(config).State = EntityState.Modified;
            }
            else
            {
                config.CreateUser = operatorUsername;
                CurrentDbContext.Entry(config).State = EntityState.Added;
            }
            result = CurrentDbContext.SaveChanges() > 0;
            #region 更新缓存
            if (CacheBll.Remove(CacheIdentity.ConfigTable))
            {
                CacheBll.Add(CacheIdentity.ConfigTable, CurrentDbContext.Configs.ToList());
            }
            #endregion

            return result;
        }

        /// <summary>
        /// 查询配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Config SearchConfig(string key)
        {
            Config result = null;

            List<Config> configList = CacheBll.GetCache<List<Config>>(CacheIdentity.ConfigTable);
            if (configList != null && configList.Count > 0)
            {
                result = configList.Find(a => a.Key == key);
            }

            return result;
        }

        /// <summary>
        /// 删除配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool DeleteConfig(string key)
        {
            bool result = false;

            Config config = new Config() { Key = key };
            CurrentDbContext.Entry(config).State = EntityState.Deleted;
            result = CurrentDbContext.SaveChanges() > 0;
            #region 更新缓存
            if (CacheBll.Remove(CacheIdentity.ConfigTable))
            {
                CacheBll.Add(CacheIdentity.ConfigTable, CurrentDbContext.Configs.ToList());
            }
            #endregion

            return result;
        }

        #region 私有方法
        /// <summary>
        /// 获取操作人用户名
        /// </summary>
        /// <returns></returns>
        private string GetOperatorUsername()
        {
            string result = "";

            string cookieValue = CookieHelper.GetCookieValue(CookieConfig.LoginCookieName);
            if (!string.IsNullOrEmpty(cookieValue))
            {
                UserInfo userinfo = CacheBll.GetCache<UserInfo>(cookieValue);
                if (userinfo != null)
                {
                    result = userinfo.LoginName;
                }
            }

            return result;
        }
        #endregion

    }
}
